package cn.z2huo.demo.mybatis.plus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.z2huo.core.exception.ServerException;
import cn.z2huo.demo.mapper.user.UserMapper;
import cn.z2huo.demo.model.dataobject.user.UserDO;
import cn.z2huo.demo.mybatis.plus.dao.UserDAO;
import cn.z2huo.demo.mybatis.plus.model.dto.user.QueryUserDTO;
import cn.z2huo.demo.mybatis.plus.model.dto.user.SaveUpdateUserDTO;
import cn.z2huo.demo.mybatis.plus.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDAO userDAO;

    @Override
    public void insert(SaveUpdateUserDTO dto) {
        UserDO UserDO = new UserDO();
        BeanUtil.copyProperties(dto, UserDO);
        int result = userMapper.insert(UserDO);
        if (result != 1) {
            throw new ServerException("插入用户数据出错");
        }
    }

    @Override
    public UserDO selectByUserCode(String userCode) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_code", userCode);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public UserDO selectById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public void deleteById(Long id) {
        int resultCode = userMapper.deleteById(id);
        if (resultCode != 1) {
            throw new ServerException("删除用户数据出错");
        }
    }

    @Override
    public void update(SaveUpdateUserDTO dto) {

    }

    @Override
    public List<UserDO> query(QueryUserDTO dto) {
        return List.of();
    }

    /**
     * 列出所有有效的用户
     */
    public List<UserDO> listAllValidUser() {
        return userDAO.listValidUser();
    }

}
